package com.jt.service;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.mapper.ItemMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.ItemVO;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ItemDescMapper itemDescMapper;

    @Override
    public PageResult getItemList(PageResult pageResult) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("title",pageResult.getQuery());
        //完成分页操作
        //准备分页对象    获取分页参数
        IPage<Item> page = new Page<>(pageResult.getPageNum(),
                                     pageResult.getPageSize());
        page=itemMapper.selectPage(page,queryWrapper);
        //获取总数
        long total = page.getTotal();
        //获取记录数
        List<Item> list = page.getRecords();
        return pageResult.setTotal(total).setRows(list);
    }

    @Override
    @Transactional
    public void updateItemStatus(Item item) {
        item.setUpdated(new Date());
        itemMapper.updateById(item);
    }

    @Override
    @Transactional
    public void saveItem(ItemVO itemVO) {
        Date date = new Date();
        Item item = new Item();
        item.setStatus(true).setCreated(date).setUpdated(date);
        itemMapper.insert(item);
        //入库详情表
        ItemDesc itemDesc = itemVO.getItemDesc();
        itemDesc.setId(item.getId()).setCreated(date).setUpdated(date);
        itemDescMapper.insert(itemDesc);
    }

    @Override
    @Transactional
    public void deleteItemById(Integer id) {
       itemMapper.deleteById(id);
       itemDescMapper.deleteById(id);
    }

}
